package com.ky.home.controller;


import cn.hutool.core.convert.NumberWithFormat;
import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ky.common.config.SystemConfig;
import com.ky.common.param.QuestionAdd;
import com.ky.common.util.JwtUtil;
import com.ky.common.vo.R;
import com.ky.home.entity.Exam;
import com.ky.home.entity.ExamLog;
import com.ky.home.entity.WrongQuestions;
import com.ky.home.mapper.ExamLogMapper;
import com.ky.home.mapper.ExamMapper;
import com.ky.home.mapper.WrongQuestionsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 * 答题表 前端控制器
 * </p>
 *
 * @author pl
 * @since 2024-06-22
 */
@RestController
@RequestMapping("/home/exam")
public class ExamController {
    @Autowired
    private ExamMapper examMapper;
    @Autowired
    private WrongQuestionsMapper wrongQuestionsMapper;
    @Autowired
    private ExamLogMapper examLogMapper;
    @Autowired
    private RedisTemplate redisTemplate;

    @RequestMapping("/searchExam")
    public R getQuestion() {
        int i = RandomUtil.randomInt(1, 40);
        QueryWrapper<Exam> examQueryWrapper = new QueryWrapper<>();
        examQueryWrapper.eq("id", i);
        Exam exam = examMapper.selectOne(examQueryWrapper);
        if (exam != null) {
            return R.ok(exam);
        }
        return R.fail("没有找到题目");
    }

    @RequestMapping("/searchYearQuestion")
    public R searchYearQuestion(HttpServletRequest request, @RequestBody QuestionAdd questionAdd) {
        String token = request.getHeader(SystemConfig.HEADER_TOKEN);
        Integer id = ((NumberWithFormat) JwtUtil.parseToken(token, "id")).intValue();
        QueryWrapper<Exam> examQueryWrapper = new QueryWrapper<>();
        examQueryWrapper.eq("id", questionAdd.getQuestionId());
        Exam exam = examMapper.selectOne(examQueryWrapper);
        ExamLog examLog = new ExamLog();
        if (exam.getRightOption().equals(questionAdd.getChecked())) {
            Exam exam1 = examMapper.selectById(questionAdd.getQuestionId());
            examLog.setYearId(exam1.getYearId())
                    .setUserId(id)
                    .setQuestionId(exam.getId())
                    .setCreateTime(new Date())
                    .setStatus("正确");
            examLogMapper.insert(examLog);
            System.out.println("插入正确记录成功");
            redisTemplate.opsForValue().set("questionid_right", exam.getId(), 24 * 60 * 60, TimeUnit.SECONDS);
            Integer questionid = (Integer) redisTemplate.opsForValue().get("questionid_right");
            System.out.println(questionid);
            return R.ok(exam);
        } else {
            WrongQuestions wrongQuestions = new WrongQuestions();
            wrongQuestions.setQuestionId(exam.getId())
                    .setUserId(id)
                    .setAnswerGiven(questionAdd.getChecked())
                    .setAnswerCorrect(exam.getRightOption())
                    .setAttemptTime(new Date())
                    .setType(2);
            redisTemplate.opsForValue().set("questionid_error", exam.getId());
            Integer yearId = (Integer) redisTemplate.opsForValue().get("yearId_error");
            examLog.setYearId(yearId)
                    .setUserId(id)
                    .setQuestionId(exam.getId())
                    .setCreateTime(new Date())
                    .setStatus("错误");
            examLogMapper.insert(examLog);
            System.out.println("插入错误记录成功");
            int insert = wrongQuestionsMapper.insert(wrongQuestions);
            if (insert > 0) {
                System.out.println("插入错题本成功");
            }
        }
        return R.fail("回答错误");
    }
}

